import { createRouter, createWebHashHistory } from 'vue-router'
//公共结构
import Layout from '@/components/Layout.vue';
//router:路由实例对象
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    // 登录
    {
      path: '/login',
      // 地址别名
      alias: '/',
      name: 'Login',
      //懒加载路由
      component: () => import('../views/Login.vue'),
      meta: { requiresAuth: false }
    },
    // 注册
    {
      path: '/register',
      // 地址别名
      alias: '/register',
      name: 'Register',
      component: () => import('../views/Register.vue'),
      meta: { requiresAuth: false }
    },
    //账户管理
    {
      path: '/',
      name: '',
      component: Layout,
      meta: { requiresAuth: true },
      children: [{
        path: '/Account',
        name: 'account',
        component: () => import('@/views/Account.vue')
      }]

    },
    // 用户管理
    {
      path: '/',
      name: '',
      component: Layout,
      meta: { requiresAuth: true },
      children: [
        {
          path: 'user',
          name: 'User',
          component: () => import('@/views/User.vue')
        },
      ]
    },
    //分类管理
    {
      path: '/category',
      name: 'category',
      component: Layout,
      meta: { requiresAuth: true },
      children: [
        {
          path: 'Classify_lazy',
          name: 'lazy',
          component: () => import('@/views/Category/Classify_lazy.vue')
        },
        {
          path: 'Classify',
          name: 'Classify',
          component: () => import('@/views/Category/Classify.vue')
        },
      ]
    },
    // 文章管理
    {
      path: '/aritcle',
      name: 'Aritcle',
      component: Layout,
      meta: { requiresAuth: true },
      children: [
        {
          path: 'list',
          name: 'Aritclelist',
          component: () => import('@/views/Aritcle/List.vue')
        },
        {
          path: 'edit/:id',
          name: 'Aritcledit',
          component: () => import('@/views/Aritcle/List_edit.vue')
        },
        {
          path: 'tag',
          name: 'Aritcletag',
          component: () => import('@/views/Aritcle/Tag.vue')
        },
        {
          path: 'release',
          name: 'Aritclerelease',
          component: () => import('@/views/Aritcle/Release.vue')
        },
        {
          path: 'comment',
          name: 'Comment',
          component: () => import('@/views/Aritcle/Comment.vue')
        },
      ]
    },
    // 推广管理
    {
      path: '/popularize',
      name: 'Popularize',
      // component: () => import('../views/Register.vue')
      component: Layout,
      meta: { requiresAuth: true },
      children: [
        {
          path: 'friend',
          name: 'friend',
          component: () => import('@/views/Popularize/Friendlink.vue')
        },
        {
          path: 'slide',
          name: 'slide',
          component: () => import('@/views/Popularize/Slideshow.vue')
        },
      ]
    },
    // 系统管理
    {
      path: '/system',
      name: 'System',
      // component: () => import('../views/Register.vue')
      component: Layout,
      meta: { requiresAuth: true },
      children: [
        {
          path: 'admin_edit/:id',
          name: 'Admin_edit',
          component: () => import('@/views/System/Admin_edit.vue')
        },
        {
          path: 'admin',
          name: 'admin',
          component: () => import('@/views/System/Admin.vue')
        },
        {
          path: 'power',
          name: 'power',
          component: () => import('@/views/System/Power.vue')
        },
        {
          path: 'menu',
          name: 'menu',
          component: () => import('@/views/System/Menu.vue')
        },
      ]
    },
    //公告管理
    {
      path: '/announcement',
      name: 'Announcement',
      // component: () => import('../views/Register.vue')
      component: Layout,
      meta: { requiresAuth: true },
      children: [
        {
          path: 'list',
          name: 'list',
          component: () => import('@/views/Announcement/List.vue')
        },
        {
          path: 'edit/:id',
          name: 'edit',
          component: () => import('@/views/Announcement/Edit.vue')
        },
        {
          path: 'add',
          name: 'add',
          component: () => import('@/views/Announcement/Add.vue')
        },
      ]
    },

  ]
})
//全局前置守卫
router.beforeEach((to, from) => {
  //判断是否需要权限登录验证
  if (!to.meta.requiresAuth) {
    return true;
  }
  //提前token校验
  let token = sessionStorage.token;
  if (!token) {
    //重定向至登录页,附带redirect参数
    return { name: 'Login', query: { redirect: to.fullPath } }
  }
})
export default router
